﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sydfdl.TMS.Tool;
using sydfdl.TMS.BLL;
using sydfdl.TMS.Model;

namespace sydfdl.TMS.Areas.SystemData.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /SystemData/Menu/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult IndexXml()
        {
            return View();
        }

        public void GetMenu()
        {
            Response.Write(JsonHelp.EntitiesToString(new BLLT_B_Menu().GetList()));
        }

        public void GetMenuXML()
        {
            Guid mid = Guid.Parse(Request.QueryString.Get("mid"));
            Response.Write(JsonHelp.EntitiesToString(new BLLT_B_Menu().GetMenuXML(mid)));
        }

        public void Save()
        {
            string json = @"[]";
            IEnumerable<T_B_Menu> add = Request.Form.Get("add") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Menu>>(Request.Form.Get("add"));
            IEnumerable<T_B_Menu> edit = Request.Form.Get("edit") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Menu>>(Request.Form.Get("edit"));
            IEnumerable<T_B_Menu> del = Request.Form.Get("del") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Menu>>(Request.Form.Get("del"));
            if (add != null)
                add.ToList().ForEach(a =>
                {
                    a.MID = Guid.NewGuid();
                    add.Where(b => b._pid == a._id).ToList().ForEach(c =>
                    {
                        c.FID = a.MID;
                    });
                });

            Response.Write(new BLLT_B_Menu().Save(add, edit, del));
        }


        public void SaveXML()
        {
            string json = @"[]";
            IEnumerable<T_B_MenuXML> add = Request.Form.Get("add") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_MenuXML>>(Request.Form.Get("add"));
            Guid mid = Guid.Parse(Request.Form.Get("mid"));
            Response.Write(new BLLT_B_Menu().SaveXML(add, mid));
        }



    }
}
